package com.wms.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wms.common.QueryPageParam;
import com.wms.common.Result;
import com.wms.entity.Goodstype;
import com.wms.entity.Notice;
import com.wms.service.GoodstypeService;
import com.wms.service.NoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/notice")
public class NoticeController {
    @Autowired
    private NoticeService noticeService;
    @PostMapping("/save")
    public Result save(@RequestBody Notice notice){
        return noticeService.save(notice)?Result.suc():Result.fail();
    }

    //删除
    @GetMapping("/del")
    public Result del(@RequestParam String id){
        return noticeService.removeById(id)?Result.suc():Result.fail();
    }

    @PostMapping("/listPage")
    public Result listPage(@RequestBody QueryPageParam query){
        HashMap param = query.getParam();
        String content = (String)param.get("content");

        Page<Notice> page = new Page();
        page.setCurrent(query.getPageNum());
        page.setSize(query.getPageSize());

        LambdaQueryWrapper<Notice> lambdaQueryWrapper = new LambdaQueryWrapper();
        if(StringUtils.isNotBlank(content) && !"null".equals(content)){
            lambdaQueryWrapper.like(Notice::getContent,content);
        }

        IPage result = noticeService.pageCC(page,lambdaQueryWrapper);
        return Result.suc(result.getRecords(),result.getTotal());
    }
    @PostMapping("/update")
    public Result update(@RequestBody Notice notice){
        return noticeService.updateById(notice)?Result.suc():Result.fail();
    }
    @GetMapping("/list")
    public Result list(){
        List list = noticeService.list();
        return Result.suc(list);
    }
}
